package com.example.sakurasay.passwordbook.data

import com.example.sakurasay.passwordbook.data.daos.IndependentPasswordDao
import com.example.sakurasay.passwordbook.data.daos.SharedPasswordDao
import com.example.sakurasay.passwordbook.data.entities.IndependentPasswordEntry
import com.example.sakurasay.passwordbook.data.entities.SharedAccount
import com.example.sakurasay.passwordbook.data.entities.SharedPassword
import com.example.sakurasay.passwordbook.data.relations.SharedPasswordWithAccounts
import com.example.sakurasay.security.EncryptionManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class PasswordBookRepository(
    private val independentPasswordDao: IndependentPasswordDao,
    private val sharedPasswordDao: SharedPasswordDao,
    private val encryptionManager: EncryptionManager
) {

    // Independent Passwords
    val allIndependentPasswords: Flow<List<IndependentPasswordEntry>> = independentPasswordDao.getAllEntries().map { passwords ->
        passwords.map { it.copy(password = encryptionManager.decrypt(it.password)) }
    }
    fun searchIndependentPasswords(query: String): Flow<List<IndependentPasswordEntry>> {
        return independentPasswordDao.searchPasswords(query).map { passwords ->
            passwords.map { it.copy(password = encryptionManager.decrypt(it.password)) }
        }
    }
    suspend fun insertIndependentPassword(entry: IndependentPasswordEntry) {
        val encryptedPassword = entry.copy(password = encryptionManager.encrypt(entry.password))
        independentPasswordDao.insert(encryptedPassword)
    }
    suspend fun updateIndependentPassword(entry: IndependentPasswordEntry) {
        val encryptedPassword = entry.copy(password = encryptionManager.encrypt(entry.password))
        independentPasswordDao.update(encryptedPassword)
    }
    suspend fun deleteIndependentPassword(entry: IndependentPasswordEntry) = independentPasswordDao.delete(entry)

    // Shared Passwords
    fun getAllSharedPasswords(): Flow<List<SharedPassword>> = sharedPasswordDao.getAllSharedPasswords()
    fun getAccountsForSharedPassword(passwordId: Int): Flow<List<SharedAccount>> = sharedPasswordDao.getAccountsForPassword(passwordId)
    fun searchSharedPasswords(query: String): Flow<List<SharedPassword>> = sharedPasswordDao.searchSharedPasswords(query)
    fun getSharedPasswordsWithAccounts(): Flow<List<SharedPasswordWithAccounts>> = sharedPasswordDao.getSharedPasswordsWithAccounts()

    suspend fun insertSharedPassword(password: SharedPassword, accounts: List<SharedAccount>) {
        val passwordId = sharedPasswordDao.insertSharedPassword(password)
        accounts.forEach { account ->
            sharedPasswordDao.insertSharedAccount(account.copy(passwordId = passwordId.toInt()))
        }
    }

    suspend fun updateSharedPassword(password: SharedPassword, accounts: List<SharedAccount>) {
        sharedPasswordDao.updateSharedPassword(password)
        // A more complex logic might be needed here to handle added/removed/updated accounts
        // For now, let's assume we are just updating the main password info
    }

    suspend fun deleteSharedPassword(password: SharedPassword) {
        // The CASCADE on delete should handle deleting associated accounts
        sharedPasswordDao.deleteSharedPassword(password)
    }
}